import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '../views/LoginPage.vue'
import RegisterPage from '../views/Register.vue'
import HomePage from '../views/HomePage.vue'
import UploadFile from '../components/UploadFile.vue'
import FileList from '../components/FileList.vue'
import FilePreview from '../components/FilePreview.vue'
import RockBack from '../components/RockBack.vue'
import MainPage from '../components/MainPage.vue'
import DashBoard from '../views/DashBoard.vue'
import ProjectManagement from '../views/ProjectManagement.vue'
import TaskManagement from '../views/TaskManagement.vue'
import UserManagement from '../views/UserManagement.vue'
import FileManagement from "@/views/FileManagement.vue";
import FileEdit from '../views/FileEdit.vue';
import FileView from '@/views/FileView.vue';
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/main',
    name: 'MainPage',
    component: MainPage,
    redirect: '/main/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'DashBoard',
        component: DashBoard
      },
      {
        path: 'project',
        name: 'ProjectManagement',
        component: ProjectManagement
      },
      {
        path: 'task',
        name: 'TaskManagement',
        component: TaskManagement
      },
      {
        path: 'user',
        name: 'UserManagement',
        component: UserManagement
      },
      {
        path: 'file',
        name: 'FileManagement',
        component: FileManagement
      },
      {
        path: 'file-view',
        name: 'FileView',
        component: FileView,
      },
      {
        path: 'file-edit', // 新增路由，用于文件编辑，:fileId是动态参数
        name: 'FileEdit',
        component: FileEdit
      },

    ]
  },
  {

    path: '/login',
    name: 'LoginPage',
    component: LoginPage
  },
  {
    path: '/register',
    name: 'RegisterPage',
    component: RegisterPage
  },
  {
    path: '/home',
    name: 'HomePage',
    component: HomePage,
    meta: { requiresAuth: true }
  },
  {
    path: '/upload',
    name: 'UploadFile',
    component: UploadFile
  },
  {
    path: '/file-list',
    name: 'FileList',
    component: FileList
  },
  {
    path: '/file-preview/:file',
    name: 'FilePreview',
    component: FilePreview
  },

  {
    path: '/rock-back',
    name: 'RockBack',
    component: RockBack
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 简单的路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user')

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router